package com.dbappsecurity.core;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import java.util.ArrayList;
import java.util.List;

abstract public class CoreBaseActivity extends AppCompatActivity {

  public static boolean isForeground = false;
  List<CoreBaseFragment> mChild = new ArrayList<>();

  @LayoutRes
  abstract protected int getLayout();

  abstract protected void onViewCreated(Intent source,Bundle savedInstanceState);


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(getLayout());
    onViewCreated(getIntent(),savedInstanceState);
  }

  protected void registerFragment(CoreBaseFragment coreBaseFragment) {
    if (!mChild.contains(coreBaseFragment)) mChild.add(coreBaseFragment);
  }

  protected void unregisterFragment(CoreBaseFragment coreBaseFragment) {
    mChild.remove(coreBaseFragment);
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean result = false;
    for (int i = 0; i < mChild.size(); i++) {
      result |= mChild.get(i).onKeyDown(keyCode, event);
    }
    return result || super.onKeyDown(keyCode, event);
  }
}
